View Javadoc
1   package org.apache.maven.surefire.common.junit48;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import org.apache.maven.surefire.NonAbstractClassFilter;
24  import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
25  import org.apache.maven.surefire.util.ScannerFilter;
26  import org.junit.experimental.runners.Enclosed;
27  
28  
29  /**
30   * Looks for additional junit48-like features
31   * @author Geoff Denning
32   * @author Kristian Rosenvold
33   */
34  public class JUnit48TestChecker
35      implements ScannerFilter
36  {
37      private final NonAbstractClassFilter nonAbstractClassFilter;
38  
39      private final JUnit4TestChecker jUnit4TestChecker;
40  
41  
42      public JUnit48TestChecker( ClassLoader testClassLoader )
43      {
44          this.jUnit4TestChecker = new JUnit4TestChecker( testClassLoader );
45          this.nonAbstractClassFilter = new NonAbstractClassFilter();
46      }
47  
48      public boolean accept( Class testClass )
49      {
50          return jUnit4TestChecker.accept( testClass ) || isAbstractWithEnclosedRunner( testClass );
51      }
52  
53      @SuppressWarnings( { "unchecked" } )
54      private boolean isAbstractWithEnclosedRunner( Class testClass )
55      {
56          return jUnit4TestChecker.isRunWithPresentInClassLoader()
57                          &&  isAbstract( testClass )
58                          && isRunWithEnclosedRunner( testClass );
59      }
60  
61      private boolean isRunWithEnclosedRunner( Class testClass )
62      {
63          @SuppressWarnings( "unchecked" ) org.junit.runner.RunWith runWithAnnotation =
64              (org.junit.runner.RunWith) testClass.getAnnotation( org.junit.runner.RunWith.class );
65          return ( runWithAnnotation != null && Enclosed.class.equals( runWithAnnotation.value() ) );
66      }
67  
68      private boolean isAbstract( Class testClass )
69      {
70          return !nonAbstractClassFilter.accept( testClass );
71      }
72  }